package com.baidu.cmsWeb.contorllers;

import com.baidu.cmsCommes.pojo.Channel;
import com.baidu.cmsCommes.pojo.ChannelDto;
import com.baidu.cmsCommes.pojo.ChannelType;
import com.baidu.cmsCommes.util.PageUtil;
import com.baidu.cmsService.services.ChannelService;
import com.sun.org.glassfish.gmbal.ParameterNames;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/6/22/20:40
 */
@Controller
@RequestMapping
public class ChannelContorller {
    @Autowired
    private ChannelService channelService;

    private Integer pid;

    @GetMapping("/admin/channel/channels")
    public String channelIndex(){
        PageUtil.initPage();
        /*List<Channel> channelList = channelService.channelList();*/
        return "channel/list";
    }

    @GetMapping("/admin/channel/channels/{cid}")
    public String channelChilds(@PathVariable("cid")Integer cid, Model model){
        pid = cid;
        List<Channel> channelList = channelService.selectChilds(cid);

        if (channelList==null)
            return "error";

        model.addAttribute("channels",channelList);

        return "channel/list_child";
    }

    @GetMapping("/admin/channel/add/")
    public String toSave(Model model){
        List<String> types = channelService.getAllType();
        model.addAttribute("channel",new Channel()).addAttribute("types",types);
        return "channel/add";
    }

    @PostMapping("/admin/channel/add/")
    public String saveChannel(String name,Integer customLink,String customLinkUrl,String type,Integer isIndex,Integer isTopNav,Integer recommend,Integer status){
        Channel channel = new Channel();
        channel.setName(name);
        channel.setCustom_link(customLink);
        channel.setCustom_link_url(customLinkUrl);
        channel.setType(ChannelType.valueOfClass(type));
        channel.setIs_index(isIndex);
        channel.setIs_top_nav(isTopNav);
        channel.setRecommend(recommend);
        channel.setStatus(status);
        channel.setPid(pid);
        Boolean flag = channelService.saveChannel(channel);

        if (flag)
            return "redirect:/admin/channel/channels/"+pid;
        else
            return "error";
    }

    @GetMapping("/admin/channel/update/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model){
        //  channel  types
        ChannelDto channel = channelService.selectChannelById(id);
        List<String> types = channelService.getAllType();

        model.addAttribute("channel",channel).addAttribute("types",types);
        return "channel/update";
    }

    @PostMapping("/admin/channel/update/{id}")
    public String updateChannel(@PathVariable("id") Integer id,String name,Integer customLink,String customLinkUrl,String type,Integer isIndex,Integer isTopNav,Integer recommend,Integer status){
        Channel channel = new Channel();
        channel.setName(name);
        channel.setCustom_link(customLink);
        channel.setCustom_link_url(customLinkUrl);
        channel.setType(ChannelType.valueOfClass(type));
        channel.setIs_index(isIndex);
        channel.setIs_top_nav(isTopNav);
        channel.setRecommend(recommend);
        channel.setStatus(status);

        Boolean flag = channelService.updateChannel(id,channel);

        if (!flag)
            return "error";

        return "redirect:/admin/channel/channels/"+pid;
    }

    @GetMapping("/admin/channel/delete//{id}")
    public String deleteChannel(@PathVariable("id") Integer id){
        channelService.deleteChannelById(id);

        return "redirect:/admin/channel/channels/"+pid;
    }

    @PostMapping("/admin/channel/channels/updateSort")
    @ResponseBody
    public Boolean updateSort(Integer[] ids){
        return channelService.updateSort(ids);
    }
}
